package com.example.multi.user.provider.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author
 * @description
 * @create 2022-05-18 10:16
 */
@RequestMapping("/server")
@Controller
public class ApiController {

    private static final Logger logger = LoggerFactory.getLogger(ApiController.class);
    @Value("${server.port}")
    private Integer serverPort;

    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    @ResponseBody
    public Object serverInfo() {
        StopWatch sw = new StopWatch();
        sw.start("server-info");
        Map<String, Object> result = new ConcurrentHashMap<>();
        result.put("server.port", serverPort);
        sw.stop();
        result.put("duration", sw.getTotalTimeNanos());
        ResponseEntity responseEntity = ResponseEntity.ok().body(result);
        Mono<ResponseEntity> mono = Mono.just(responseEntity).cache(Duration.ofSeconds(5));
        return mono;
    }
}
